package com.party.service.study.impl;

import com.party.common.Page;
import com.party.mapper.User2CurriculumMapper;
import com.party.model.*;
import com.party.service.study.Curriculum2contentService;
import com.party.service.study.User2curriculumService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 011926 on 2014/9/23.
 */
@Repository
public class User2CurriculumServiceImpl implements User2curriculumService
{
    @Resource
    User2CurriculumMapper user2CurriculumMapper;


    @Override
    public int add(Integer uid,Curriculum curriculum) {
        if(uid != null && uid > 0 && curriculum != null)
        {
            User2Curriculum obj = new User2Curriculum();
            obj.setCurriculumid(curriculum.getId());
            obj.setUid(uid);
            //判断数据库里没有这条数据，然后在添加
            //如果有这个条数据，则将 备用字段1置为1
            List<User2Curriculum> uc = this.getAll(obj);
            if(uc.size()>0)
            {
                uc.get(0).setSpare1("1");
                return this.update(uc.get(0));
            }
            else
            {
                obj.setIsfinished("0");
                obj.setScore(0);//得分
                obj.setSpare1("1");
                obj.setMaxscore(curriculum.getScore());//满分分数
                return this.user2CurriculumMapper.insert(obj);
            }

        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        if(id != null && id > 0 )
        {
            User2Curriculum uc = this.user2CurriculumMapper.selectByPrimaryKey(id);
            if(uc != null)
            {
                uc.setSpare1("0");//将备用字段1 设置为0,代表删除
                return this.user2CurriculumMapper.updateByPrimaryKeySelective(uc);
            }

        }
        return 0;
    }

    @Override
    public int deleteAllByCurrId(Integer id) {
        if(id != null && id > 0)
        {
            User2CurriculumExample example = new User2CurriculumExample();
            User2CurriculumExample.Criteria criteria = example.createCriteria();
            criteria.andCurriculumidEqualTo(id);
            List<User2Curriculum> ucList = this.user2CurriculumMapper.selectByExample(example);
            if(ucList != null && ucList.size() >0)
            {
                for(User2Curriculum uc:ucList)
                {
                    uc.setSpare1("0");//将备用字段1 设置为0,代表删除
                    this.user2CurriculumMapper.updateByPrimaryKeySelective(uc);
                }
                return ucList.size();
            }
        }
        return 0;
    }

    @Override
    public int deleteAllByUid(Integer id) {
        if(id != null && id > 0)
        {
            User2CurriculumExample example = new User2CurriculumExample();
            User2CurriculumExample.Criteria criteria = example.createCriteria();
            criteria.andUidEqualTo(id);
            List<User2Curriculum> ucList = this.user2CurriculumMapper.selectByExample(example);
            if(ucList != null && ucList.size() >0)
            {
                for(User2Curriculum uc:ucList)
                {
                    uc.setSpare1("0");//将备用字段1 设置为0,代表删除
                    this.user2CurriculumMapper.updateByPrimaryKeySelective(uc);
                }
                return ucList.size();
            }
        }
        return 0;
    }

    @Override
    public int update(User2Curriculum obj) {
        if(obj != null && obj.getId() != null && !"".equals(obj.getId()))
        {
            return this.user2CurriculumMapper.updateByPrimaryKeySelective(obj);
        }
        return 0;
    }

    @Override
    public List<User2Curriculum> getAll(User2Curriculum obj) {
        if(obj != null )
        {
            obj.setSpare1("");
            return this.privateGet(obj);
        }
        return null;
    }
    @Override
    public List<User2Curriculum> getFail(User2Curriculum obj) {
        if(obj != null )
        {
            obj.setSpare1("0");
            return this.privateGet(obj);
        }
        return null;
    }
    @Override
    public List<User2Curriculum> getEffect(User2Curriculum obj) {
        if(obj != null )
        {
            obj.setSpare1("1");
            return this.privateGet(obj);
        }
        return null;
    }
    @Override
    public boolean update(Integer uid, List<Curriculum> curriculumList) {
        if(uid != null && uid > 0)
        {
            User2Curriculum uc = new User2Curriculum();
            uc.setUid(uid);
            List<User2Curriculum> oldList = this.getAll(uc);
//            List<User2Curriculum> delList = new ArrayList<User2Curriculum>();//需要删除的
            if(curriculumList == null)
            {
                curriculumList = new ArrayList<Curriculum>();
            }
            if(curriculumList != null && curriculumList .size() > 0)
            {
                //修改原来有的数据
                for(User2Curriculum ou: oldList)
                {
                    boolean isStay = false;
                    for(int i=0; i< curriculumList.size(); i++)
                    {
                        Curriculum nu = curriculumList.get(i);
                        if(ou.getCurriculumid() == nu.getId())
                        {
                            isStay = true;
                            ou.setSpare1("1");//启用
                            this.user2CurriculumMapper.updateByPrimaryKeySelective(ou);
                            curriculumList.remove(i);//删除已经处理过的，剩下来的都是需要新增的
                            break;
                        }
                    }
                    //到最后都还未找到匹配的，则删除
                    if(!isStay)
                    {
                        ou.setSpare1("0");//禁用
                        this.user2CurriculumMapper.updateByPrimaryKeySelective(ou);
                    }
                }

                //新增
                for(int i=0; i< curriculumList.size(); i++)
                {
                    this.add(uid,curriculumList.get(i));
                }
                return true;
            }

        }
        return false;
    }

    @Override
    public User2Curriculum get(Integer uid, Integer curriculumId) {
        if(uid != null && uid >0 && curriculumId != null && curriculumId > 0)
        {
            User2CurriculumExample example = new User2CurriculumExample();
            User2CurriculumExample.Criteria criteria = example.createCriteria();
            criteria.andUidEqualTo(uid);
            criteria.andCurriculumidEqualTo(curriculumId);
            List<User2Curriculum> list = this.user2CurriculumMapper.selectByExample(example);
            if(list != null && list.size() > 0)
            {
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    public Page getList(User2Curriculum user2curriculum, int pageSize, int pageIndex) {
        if(user2curriculum == null )
        {
            user2curriculum = new User2Curriculum();
            user2curriculum.setRealname("");
            user2curriculum.setCurrTitle("");
            user2curriculum.setPassport("");
            user2curriculum.setqMaxScore(Integer.MAX_VALUE);
            user2curriculum.setqMinScore(-1);
        }
        else
        {
            if(user2curriculum.getCurrTitle() == null || "".equals(user2curriculum.getCurrTitle()))
            {
                user2curriculum.setCurrTitle("");
            }
            if(user2curriculum.getRealname() == null || "".equals(user2curriculum.getRealname()))
            {
                user2curriculum.setRealname("");
            }
            if(user2curriculum.getPassport() == null || "".equals(user2curriculum.getPassport()))
            {
                user2curriculum.setPassport("");
            }
            if(user2curriculum.getqMaxScore() == null)
            {
                user2curriculum.setqMaxScore(Integer.MAX_VALUE);
            }
            if(user2curriculum.getqMinScore() == null)
            {
                user2curriculum.setqMinScore(-1);
            }
        }
        //获取总的记录数量
        int count = this.user2CurriculumMapper.countByUser2Curr(user2curriculum);
        //获取指定的页码数据
        user2curriculum.setBeginNum((pageIndex - 1) * pageSize);
        user2curriculum.setEndNum(pageSize);
        List<User2Curriculum> list = this.user2CurriculumMapper.getListPage(user2curriculum);
        //组合返回page
        Page page = new Page(pageIndex, count, pageSize, list);
        return page;
    }

    private List<User2Curriculum> privateGet(User2Curriculum obj) {
        if(obj != null )
        {
            User2CurriculumExample example = new User2CurriculumExample();
            User2CurriculumExample.Criteria criteria = example.createCriteria();
            if(obj.getUid() != null && obj.getUid() > 0)
            {
                criteria.andUidEqualTo(obj.getUid());
            }
            if(obj.getCurriculumid() != null && obj.getCurriculumid() > 0)
            {
                criteria.andCurriculumidEqualTo(obj.getCurriculumid());
            }
            if(obj.getSpare1() != null && !"".equals(obj.getSpare1()))
            {
                criteria.andSpare1EqualTo(obj.getSpare1());
            }
            return this.user2CurriculumMapper.selectByExample(example);
        }
        return null;
    }
}
